import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class update {
    public static void main(String[] args) throws SQLException {
        // 1. 创建数据源对象
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("zzl666666");

        // 2. 与数据库服务器建立连接
        Connection connection = dataSource.getConnection();

        // 3. 构造 SQL
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要修改的内容的 id: ");
        int id = scanner.nextInt();
        System.out.println("输入修改后的 name: ");
        String name = scanner.next();

        String sql = "update student set name = (?) where id = (?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(2,id);
        preparedStatement.setString(1,name);

        // 4. 执行 SQL
        int n = preparedStatement.executeUpdate();
        System.out.println("n = " + n);

        // 5. 关闭连接等资源
        preparedStatement.close();
        connection.close();

    }
}
